////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.calendar.display {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	
	import kiwi.components.ComponentSkin;
	import kiwi.components.skins.IButtonSkin;
	
	import plantool.api.DateApi;
	import plantool.events.DateEvent;
	import plantool.helpers.TextFieldHelper;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class WeekButtonSkin extends ComponentSkin implements IButtonSkin {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _tf:TextField		= null;
		private var _width:Number		= 20;
		private var _height:Number		= 20;
		private var _text:String	 	= "";
		private var _activeWeek:Boolean	= false;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		private function get dateApi():DateApi { return DateApi.getInstance(); }
		public override function set text(value:String):void { _text = value; }
		public override function get text():String { return _text; invalidate(); }
		public override function get width():Number { return _width; }
		public override function set width(value:Number):void { _width = value; invalidate(); }
		public override function get height():Number { return _height; }
		public override function set height(value:Number):void { _height = value; invalidate(); }
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function WeekButtonSkin() {
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onDate_changed(e:DateEvent):void {
			//check if this is the active week
			_activeWeek = (dateApi.activeWeek == parseInt(_text));
			
			//invalidate
			invalidate();
		}
		
		protected override function init():void {
			//create textfield
			_tf = TextFieldHelper.createInfoTf(TextFieldAutoSize.NONE, TextFormatAlign.CENTER, false, false, 0);
			addChild(_tf);
			
			//listen to date change event
			dateApi.addEventListener(DateEvent.DATE_CHANGED		, onDate_changed);
			onDate_changed(null);
			
			//super init
			super.init();
		}
		
		protected override function update():void {
			//set textfield
			_tf.width 	= _width;
			_tf.y 		= (_height - _tf.height) / 2;
			_tf.text 	= _text;
			
			//set bgcolor if this is the active week
			graphics.clear();
			_tf.textColor = 0;
			if (_activeWeek) {
				graphics.beginFill(0);
				graphics.drawRect(0, 0, width, height);
				graphics.endFill();
			}
			
			if (dateApi.calculateWeekNumber(new Date()) == parseInt(_text)) {
				_tf.textColor = 0xFF6600;
			} else if (_activeWeek) {
				_tf.textColor = 0xFFFFFF;
			} else {
				_tf.textColor = 0;
			}
		}
		
		public function mouseHandler(event:MouseEvent):void {
			switch (event.type) {
				case MouseEvent.MOUSE_OVER:
					_tf.textColor = 0xFF6600;
					break;
				case MouseEvent.MOUSE_OUT:
					if (dateApi.calculateWeekNumber(new Date()) == parseInt(_text)) {
						_tf.textColor = 0xFF6600;
					} else if (_activeWeek) {
						_tf.textColor = 0xFFFFFF;
					} else {
						_tf.textColor = 0;
					}
					break;
			}
		}
	}
}